Use case: User application program in polling mode ¶
Use case applicable in HAL2 for both FLASH and NVM.
Note
In HAL2, FLASH programming APIs have been streamlined with clearer and more flexible parameters.
The legacy
TypeProgram,
FlashAddress, and
DataAddress
pattern is replaced by buffer-based APIs.
HAL2 provides specialized programming APIs like
HAL_FLASH_{area}_ProgramByAddr
for specific FLASH regions such as OTP, EDATA, and OBK.
HAL_FLASH_ProgramByAddrto program a user flash areaHAL_FLASH_OTP_ProgramByAddrto program an OTP flash areaHAL_FLASH_EDATA_ProgramByAddrto program an EDATA flash areaHAL_FLASH_OBK_ProgramByAddrto program an OB Key flash area
Sequence diagram ¶
Important
In HAL2, the programming APIs handle the entire data buffer in a single call, eliminating the need for explicit looping over FLASH units.
Code snippet ¶
|
HAL1 |
HAL2 |
|---|---|
uint32_t Address = FLASH_USER_START_ADDR;
const uint32_t QUADWORD[4] = {
0x12345678, 0x87654321, 0x12344321, 0x56788765
};
/* Unlock the FLASH to enable the FLASH control register access */
HAL_FLASH_Unlock();
while (Address < FLASH_USER_END_ADDR)
{
/* Program FLASH memory */
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_QUADWORD,
Address,
(uint32_t)QUADWORD) == HAL_OK)
{
Address = Address + 32U; /* increment for the next FLASH word */
}
}
/* Lock the FLASH to disable the FLASH control register access */
HAL_FLASH_Lock();
|
hal_flash_handle_t flash_handle;
uint32_t address = FLASH_START_ADDR;
/* FLASH initialization */
HAL_FLASH_Init(&flash_handle, HAL_FLASH);
/* Unlock the FLASH to enable the FLASH control register access */
HAL_FLASH_ITF_Unlock(HAL_FLASH);
#if defined (USE_HAL_FLASH_PROGRAM_BY_ADDR) && (USE_HAL_FLASH_PROGRAM_BY_ADDR == 1U)
/* Set the programming mode according to the FLASH granularity */
HAL_FLASH_SetProgrammingMode(&flash_handle, HAL_FLASH_PROGRAM_QUADWORD);
/* Program FLASH memory using the granularity size in bytes */
/* area = User flash, EDATA, OTP */
HAL_FLASH_ProgramByAddr(&flash_handle, address, data_buf, QUADWORD_SIZE_BYTES, TIMEOUT);
#endif /* USE_HAL_FLASH_PROGRAM_BY_ADDR */
/* Lock the FLASH to disable the FLASH control register access */
HAL_FLASH_ITF_Lock(HAL_FLASH);
/* FLASH deinitialization */
HAL_FLASH_DeInit(&flash_handle);
|
Key changes ¶
Buffer-based programming replaces the HAL1 per-granule loop (single call programs a full buffer).
Programming APIs are specialized per area in HAL2 (USER/OTP/EDATA/OBK) via
HAL_FLASH_{area}_ProgramByAddr.HAL2 uses an explicit handle + IO/ITF split (init/deinit in IO, unlock/lock in ITF).

